package cn.kimming.rpc.common.util;

import org.springframework.util.StringUtils;

public class RpcProperty {


    public static RpcPropertyBuilder builder()  {
        RpcPropertyBuilder builder = new RpcPropertyBuilder();
        return builder;
    }

    public static class RpcPropertyBuilder {
        private String systemPropertyName; // JVM属性对应名称
        private String systemEnvName; // ENV对应名称
        private Object defaultValue; // 默认值

        public RpcPropertyBuilder systemPropertyName(String systemPropertyName) {
            this.systemPropertyName = systemPropertyName;
            return this;
        }

        public RpcPropertyBuilder systemEnvName(String systemEnvName) {
            this.systemEnvName = systemEnvName;
            return this;
        }

        public <T> RpcPropertyBuilder defaultValue(T defaultValue) {
            this.defaultValue = defaultValue;
            return this;
        }

        public <T> T get() {
            String value = System.getProperty(systemPropertyName);
            if (StringUtils.hasText(value)) {
                return (T) value;
            }
            value = System.getenv(systemEnvName);
            if (StringUtils.hasText(value)) {
                return (T) value;
            }
            return (T) defaultValue;
        }
    }


}
